﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
        private int depth = 10;
        private double th1 = 30 * Math.PI / 180;
        private double th2 = 20 * Math.PI / 180;
        private double per1 = 0.6;
        private double per2 = 0.7;
        private double leng = 100;
        private Pen pen;

        public Form1()
        {
            InitializeComponent();
            graphics = this.CreateGraphics();
            pen = new Pen(Color.Blue, 2);
        }

        private void drawCayleyTree(Graphics graphics, int n, double x0, double y0, double leng, double th, Color color, float penWidth)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(graphics, x0, y0, x1, y1, color, penWidth);

            drawCayleyTree(graphics, n - 1, x1, y1, per1 * leng, th + th1, color, penWidth);
            drawCayleyTree(graphics, n - 1, x1, y1, per2 * leng, th - th2, color, penWidth);
        }

        private void drawLine(Graphics graphics, double x0, double y0, double x1, double y1, Color color, float penWidth)
        {
            using (Pen pen = new Pen(color, penWidth))
            {
                graphics.DrawLine(pen, (int)x0, (int)y0, (int)x1, (int)y1);
            }
        }


        private void btnDraw_Click_1(object sender, EventArgs e)
        {
            try
            {
                depth = (int)nudDepth.Value;
                leng = (double)nudLength.Value;
                per1 = (double)nudRight.Value;
                per2 = (double)nudLeft.Value;
                th1 = (double)nudRightAngle.Value * Math.PI / 180;
                th2 = (double)nudLeftAngle.Value * Math.PI / 180;
                pen = new Pen(cpPenColor.Color, (float)nudPenWidth.Value);
                graphics.Clear(this.BackColor);
                float penWidth = (float)nudPenWidth.Value;

                ColorDialog cd = new ColorDialog();
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    Color color = cd.Color;

                    Form form = new Form();
                    form.StartPosition = FormStartPosition.CenterScreen;
                    form.Text = "Cayley Tree";
                    form.ClientSize = new Size(800, 600);
                    form.Paint += (s, evt) =>
                    {
                        Graphics graphics = evt.Graphics;
                        drawCayleyTree(graphics, depth, form.ClientSize.Width / 2, form.ClientSize.Height - 50, leng, -Math.PI / 2, color, penWidth);
                    };
                    form.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
